<template>
    <div class="app-container">
       <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
          <el-form-item label="用户ID" prop="keyword">
             <el-input
                v-model="queryParams.keyword"
                placeholder="请输入"
                clearable
                style="width: 200px"
                @keyup.enter="handleQuery"
             />
          </el-form-item>
          <el-form-item>
             <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
             <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
       </el-form>
 
       <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
             <el-button
                type="primary"
                plain
                icon="Plus"
                @click="handleAdd"
             >新增</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
       </el-row>
 
       <el-table v-loading="loading" :data="friendList">
          <el-table-column prop="id" label="" width="100">
            <template #default="scope">
                <span class="idx"  :style="{ backgroundColor: scope.$index < 3 ? 'black' : 'gray' }">
                  {{scope.$index + 1}}
                </span>
              </template>
          </el-table-column>
          <el-table-column prop="userID" label="用户ID">
            <template #default="scope">
              {{scope.row.userID}}
            </template>
          </el-table-column>
          <el-table-column prop="nickname" label="用户昵称">
            <template #default="scope">
              {{scope.row.user.nickname}}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="210" class-name="small-padding fixed-width">
            <template #default="scope">
                <el-button link type="primary" @click="handleDelete(scope.row)">移除</el-button>
            </template>
          </el-table-column>
       </el-table>
       <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="pageObj.pageNumber"
          v-model:limit="pageObj.showNumber"
          @pagination="getList"
       /> 
 
       <!-- 添加或修改菜单对话框 -->
       <el-dialog :title="title" v-model="open" width="680px" append-to-body>
          <SelectUserDialog
              title=""
              :table-data="tableData"
              @submit="handleSubmitUsers"
              v-model="open"
              ref="selectUser"
          />
          <template #footer>
            <div class="dialog-footer">
              <el-button @click="cancel">取 消</el-button>
              <el-button type="primary" @click="submit">确 定</el-button>
            </div>
          </template> 
        </el-dialog>
    </div>
 </template>
 
 <script setup name="defaultFriend">
 import { getDefaultFriendList, delDefaultFriend, addDefaultFriend } from "@/api/chat/defaultFriend";
 import SelectUserDialog from '@/components/SelectUserDialog';

 
 const { proxy } = getCurrentInstance();
 
 const friendList = ref([]);
 const open = ref(false);
 const loading = ref(true);
 const showSearch = ref(true);
 const title = ref("");
 const total = ref(0);
 const tableData = [];
 const selectUser = ref()
 
 const data = reactive({
   form: {},
   queryParams: {
     current: 1,
     pageSize: 10,
     keyword: undefined
   },
   pageObj:{
      pageNumber: 1,
      showNumber: 10,
   },
   rules: {
     menuName: [{ required: true, message: "菜单名称不能为空", trigger: "blur" }],
     orderNum: [{ required: true, message: "菜单顺序不能为空", trigger: "blur" }],
     path: [{ required: true, message: "路由地址不能为空", trigger: "blur" }]
   },
 });
 
 const { queryParams, form, rules, pageObj } = toRefs(data);
 
 /** 查询菜单列表 */
 function getList() {
   loading.value = true;
   const parmas = {
      pagination: data.pageObj,
      ...data.queryParams
   }
   getDefaultFriendList(parmas).then(response => {
     friendList.value = response.data.users;
     total.value = response.data.total;
     loading.value = false;
   });
 }
 
 /** 取消按钮 */
 function cancel() {
   open.value = false;
   reset();
 }
 function handleSubmitUsers (selectArray) { 
  const selectIDs = selectArray.map(item =>item.userID)
  const params = {
    userIDs: selectIDs
  }
  addDefaultFriend(params).then(response => {
    proxy.$modal.msgSuccess("新增成功");
    getList();
    open.value = false;
  });
 }
 
 /** 表单重置 */
 function reset() {
   form.value = {
     menuId: undefined,
     parentId: 0,
     menuName: undefined,
     icon: undefined,
     menuType: "M",
     orderNum: undefined,
     isFrame: "1",
     isCache: "0",
     visible: "0",
     status: "0"
   };
   proxy.resetForm("menuRef");
 }
 
 /** 搜索按钮操作 */
 function handleQuery() {
   getList();
 }
 
 /** 重置按钮操作 */
 function resetQuery() {
   proxy.resetForm("queryRef");
   handleQuery();
 }
 
 /** 新增按钮操作 */
 function handleAdd(row) {
   open.value = true;
   title.value = "";
 }
 /** 删除按钮操作 */
 function handleDelete(row) {
   const roleIds = row.userID;
   const params = {
    userIDs:[roleIds]
   }
   proxy.$modal.confirm('是否确认移除该用户').then(function () {
     return delDefaultFriend(params);
   }).then(() => {
     getList();
     proxy.$modal.msgSuccess("移除成功");
   }).catch(() => {});
 }
 /** 选择用户提交 */  
 function submit(){
   selectUser.value.submit()
 }
 getList();
 </script>
 